﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TapTapServer
{
    public class ThemeManager : BackgroundWorker
    {
        public ThemeManager()
            : base()
        {
            WorkerReportsProgress = true;
            WorkerSupportsCancellation = true;
        }

        public void processDirectory(string containingFolder)
        {
            RunWorkerAsync(new workerArgs(containingFolder, true));
        }

        protected override void OnDoWork(DoWorkEventArgs e)
        {
            workerArgs args = (workerArgs)e.Argument;
            List<string> themePaths = new List<string>();
            if (!args.isContainingFolder)
            {
                themePaths.Add(args.folder);
            }
            else
            {
                themePaths.AddRange(Directory.GetDirectories(args.folder));
            }

            int count = 0;
            foreach (string selectedPath in themePaths)
            {
                if (selectedPath.Contains("JessieJ"))
                {
                }
                if (isValidTheme(selectedPath))
                {
                    string zipPath = makePathToZip(selectedPath);
                    zipManager.addFilesToNewZip(zipPath, selectedPath);
                }
                else
                {
                    Console.WriteLine("Invalid Tap Tap Theme selected");
                }
                count++;
                float progress = ((float)count / (float)themePaths.Count) * 100f;
                ReportProgress((int)progress, "Processed Theme: " + getNameOfTheme(selectedPath));
                if (CancellationPending) break;
            }

        }

        private bool isValidTheme(string path)
        {
            //assume every valid theme containts an Info.plist (it is needed later on)
            if (File.Exists(Path.Combine(path, "Info.plist")))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private string getNameOfTheme(string path)
        {
            return Path.GetFileNameWithoutExtension(path);
        }
        private int getThemeVersion(string path)
        {
            StreamReader InfoReader = File.OpenText(Path.Combine(path, "Info.plist"));
            string Info = InfoReader.ReadToEnd();
            InfoReader.Close();
            Info = Info.Substring(Info.IndexOf("kTTRDownloadableItemVersionKey"));
            Info = Info.Substring(Info.IndexOf("<integer>") + 9);
            Info = Info.Substring(0, Info.IndexOf("</integer>"));

            return int.Parse(Info);
        }
        private string makePathToZip(string selectedPath)
        {
            if (!Directory.Exists(Settings.Instance.ThemePath)) Directory.CreateDirectory(Settings.Instance.ThemePath);

            string zipPath = Settings.Instance.ThemePath;
            zipPath = Path.Combine(zipPath, getNameOfTheme(selectedPath) + "Bundle");
            if (!Directory.Exists(zipPath)) Directory.CreateDirectory(zipPath);
            zipPath = Path.Combine(zipPath, "v" + getThemeVersion(selectedPath).ToString());
            if (!Directory.Exists(zipPath)) Directory.CreateDirectory(zipPath);
            zipPath = Path.Combine(zipPath, getNameOfTheme(selectedPath) + ".ttrTheme.zip");

            //Console.WriteLine("Will put zip file at " + zipPath);
            return zipPath;
        }

    }

}
